package com.swiftops.basic.sys.service.impl;

import com.swiftops.common.constant.CommonEnum;
import com.swiftops.common.service.AbstractService;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.util.IdGen;
import com.swiftops.common.util.ListUtil;
import com.swiftops.common.constant.CommonConstant;
import com.swiftops.basic.sys.dao.SysRoleMapper;
import com.swiftops.basic.sys.model.SysRole;
import com.swiftops.basic.sys.service.SysRoleService;
import com.swiftops.basic.sys.vo.entity.SysRoleEntityVO;
import com.swiftops.basic.sys.vo.request.SysRoleRequestVO;
import com.swiftops.basic.sys.vo.response.SysRoleResponseVO;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * 角色
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@Service
@Transactional
public class SysRoleServiceImpl extends AbstractService<SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 获取角色分页列表
     *
     * @param sysRoleRequestVO 角色查询条件
     * @return pageResult 角色分页结果
     */
    @Override
    public PageResult<SysRoleResponseVO> querySysRolePageList(SysRoleRequestVO sysRoleRequestVO) {
        //
        if (sysRoleRequestVO.getPageNumber() == null) sysRoleRequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (sysRoleRequestVO.getPageSize() == null) sysRoleRequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<SysRoleResponseVO> sysRoleResponseVOList = Collections.emptyList();
        Condition condition = new Condition(SysRole.class);
        Example.Criteria criteria = condition.createCriteria();
        //
        //if (StringUtils.isNotBlank(sysRoleRequestVO.getDesc())) {
        //    criteria.andLike("userName", "%" + sysRoleRequestVO.getDesc() + "%");
        //}
        //删除状态
        criteria.andEqualTo("deleteFlag", CommonEnum.StateFlagEnum.STATE_FLAG_NORMAL.getCode());
        //
        int total = this.selectCountByCondition(condition);
        if (total > 0) {
            PageHelper.startPage(sysRoleRequestVO.getPageNumber(), sysRoleRequestVO.getPageSize());
            List<SysRole> sysRoleList = this.selectByCondition(condition);
            sysRoleResponseVOList = ListUtil.convertList(sysRoleList, SysRoleResponseVO.class);
        }
        //
        return PageResult.of(total, sysRoleResponseVOList);
    }

    /**
     * 获取角色详细信息
     *
     * @param id 角色ID
     * @return 角色
     */
    @Override
    public SysRoleEntityVO getSysRoleEntityVO(String id) {
        SysRole sysRole = this.selectByPrimaryKey(id);
        if (null == sysRole) return null;

        SysRoleEntityVO sysRoleEntityVO = new SysRoleEntityVO();
        BeanUtils.copyProperties(sysRole, sysRoleEntityVO);
        return sysRoleEntityVO;
    }

    /**
     * 添加角色
     *
     * @param sysRoleEntityVO 角色
     */
    @Override
    public void addSysRole(SysRoleEntityVO sysRoleEntityVO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleEntityVO, sysRole);
        sysRole.setId(IdGen.next());
        this.insertSelective(sysRole);
    }

    /**
     * 修改角色
     *
     * @param sysRoleEntityVO 角色
     */
    @Override
    public void updateSysRole(SysRoleEntityVO sysRoleEntityVO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleEntityVO, sysRole);
        this.updateByPrimaryKeySelective(sysRole);
    }


    /**
     * 删除角色
     *
     * @param id 角色ID
     */
    @Override
    public void deleteSysRole(String id) {
        SysRole sysRole = new SysRole();
        sysRole.setId(id);
        //sysRole.setStateFlag(CommonEnum.StateFlagEnum.STATE_FLAG_DELETE.getCode());
        //逻辑删除
        this.updateByPrimaryKeySelective(sysRole);
    }
}
